package com.akk.platform.manage.business;

import com.akk.common.enums.AdminEnums;
import com.akk.common.enums.AdminRoleEnums;
import com.akk.module.admin.entity.Admin;
import com.akk.module.admin.entity.AdminRole;
import com.akk.module.admin.model.response.AdminRes;
import com.akk.module.admin.service.AdminRoleService;
import com.akk.module.admin.service.AdminService;
import com.akk.module.app.service.AppService;
import com.akk.utils.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.*;

import static com.akk.utils.Constants.PLATFORM_MANAGE_SUPER_ROLE_ID;
import static com.akk.utils.RestResponse.PLATFORM_ERROR_CODE;


@Service
public class AdminBusiness {

    @Autowired
    AdminService adminService;
    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AppService appService;

    public RestResponse adminList(Query query,Admin loginAdmin) {
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AdminRes> admins = adminService.findByParam(loginAdmin.getAdminId());
        return RestResponse.build(PageUtils.buildPage(admins,(int) new PageInfo<>(admins).getTotal(),query));
    }

    public RestResponse adminAdd(Admin admin,Admin loginAdmin) {
        if (StringUtils.isEmpty(admin.getRoleId())){
            return RestResponse.build(PLATFORM_ERROR_CODE,"管理账户必须拥有角色");
        }
        admin.setAdminStatus(AdminEnums.StatusEnum.NORMAL.getValue());
        admin.setFromAdminId(loginAdmin.getAdminId());
        admin.setAdminId(IDUtils.getPK());
        admin.setPassword(new Sha256Hash(admin.getPassword()).toHex());
        admin.setCreateDate(new Date());
        admin.setUpdateDate(new Date());
        AdminRole loginAdminRole = adminRoleService.findByRoleId(loginAdmin.getRoleId());
        if(loginAdminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.MANAGE.getValue()){
            AdminRole adminRole = adminRoleService.findByRoleId(admin.getRoleId());
            if(adminRole == null||adminRole.getRoleId().equals(loginAdmin.getRoleId())){
                return RestResponse.build(PLATFORM_ERROR_CODE,"角色选择非法");
            }
            if(adminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.MANAGE.getValue()){
                //平台管理员开平台成员
                if(!StringUtils.isEmpty(admin.getAppId())){
                    return RestResponse.build(PLATFORM_ERROR_CODE,"平台成员无APP编号.");
                }
                admin.setAdminType(AdminEnums.TypeEnum.MANAGE.getValue());
                adminService.save(admin);
                return RestResponse.build(admin);
            }
            if(adminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.OEM.getValue()){
                //平台管理员开OEM管理员
                if(StringUtils.isEmpty(admin.getAppId()) || appService.findByAppId(admin.getAppId())==null){
                    return RestResponse.build(PLATFORM_ERROR_CODE,"APP编号未找到");
                }
                admin.setAdminType(AdminEnums.TypeEnum.OEM.getValue());
                adminService.save(admin);
                return RestResponse.build(admin);
            }
        }
        if(loginAdminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.OEM.getValue()) {
            AdminRole adminRole = adminRoleService.findByRoleId(admin.getRoleId());
            if(adminRole == null||adminRole.getRoleId().equals(loginAdmin.getRoleId())){
                return RestResponse.build(PLATFORM_ERROR_CODE,"角色选择非法");
            }
            if(adminRole.getRoleType().intValue() == AdminRoleEnums.TypeEnum.OEM.getValue()){
                //OEM管理员开OEM成员
                admin.setAppId(loginAdmin.getAppId());
                admin.setAdminType(AdminEnums.TypeEnum.OEM.getValue());
                adminService.save(admin);
                return RestResponse.build(admin);
            }
        }
        return RestResponse.build(PLATFORM_ERROR_CODE,"操作失败");
    }

    public RestResponse adminUpdate(Admin admin) {
        //管理员修改手机，密码，用户名
        Admin admin1 = adminService.findByAdminId(admin.getAdminId());
        if (!Md5Utils.md5_low(admin1.getPassword()).equals(admin.getPassword()))
            admin1.setPassword( new Sha256Hash(admin.getPassword()).toHex());
        admin1.setUpdateDate(new Date());
        admin1.setUsername(admin.getUsername());
        admin1.setPhone(admin.getPhone());
        adminService.update(admin1);
        return RestResponse.build("操作成功");
    }

    public RestResponse adminDel(String adminId,Admin loginAdmin) {
        if(loginAdmin.getAdminId().equals(adminId) || adminId.equals(Constants.PLATFORM_MANAGE_ADMIN_ID))
            return RestResponse.build(PLATFORM_ERROR_CODE,"不能删除该管理员");
        adminService.deleteByAdminId(adminId);
        return RestResponse.build("操作成功");
    }

    public RestResponse adminInfo(String adminId) {
        return RestResponse.build(adminService.findByAdminId(adminId));
    }
}
